package com.learn.project2.controller.admin;

import com.learn.project2.model.bo.admin.role.PostRolePermissionBO;
import com.learn.project2.model.bo.admin.role.RoleCreateBO;
import com.learn.project2.model.bo.admin.role.RoleDeleteBO;
import com.learn.project2.model.bo.admin.role.RoleUpDateBO;
import com.learn.project2.model.vo.admin.RoleListVO;
import com.learn.project2.model.vo.admin.role.RoleCreateVO;
import com.learn.project2.model.vo.admin.role.RoleOptionsVO;
import com.learn.project2.model.vo.admin.role.RolePermissionsVO;
import com.learn.project2.service.admin.role.RoleService;
import com.learn.project2.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Classname RoleController
 * @Description TODO 角色管理 #61-66
 * @author王茁
 * @Date 2022/1/6 21:33
 */
@RestController()
@RequestMapping("admin/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @RequestMapping("list")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-06 21:39
     * @version 1.0
     */
    public Result list(Integer page, Integer limit, String name, String sort, String order) {
        RoleListVO list = roleService.list(page, limit, name, sort, order);
        return Result.ok(list);
    }

    @RequestMapping("options")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-06 21:39
     * @version 1.0
     */
    public Result options() {
        RoleOptionsVO roleOptionsVO = roleService.getOptions();
        return Result.ok(roleOptionsVO);
    }

    @PostMapping("create")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-06 21:39
     * @version 1.0
     */
    public Result create(@RequestBody RoleCreateBO roleCreateBO) {
        if (roleCreateBO.getName() != null) {
            RoleCreateVO roleCreateVO = roleService.create(roleCreateBO);
            if (roleCreateVO != null) {
                return Result.ok(roleCreateBO);
            }
            return Result.fail("角色已经存在");
        }
        return Result.fail("用户名不能为空");
    }

    @GetMapping("permissions")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-06 21:39
     * @version 1.0
     */
    public Result getPermissions(Integer roleId) {
        RolePermissionsVO permissions = roleService.getPermissions(roleId);
        return Result.ok(permissions);
    }

    @PostMapping("permissions")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-06 21:39
     * @version 1.0
     */
    public Result postPermissions(@RequestBody PostRolePermissionBO postRolePermissionBO) {
        int roleId = postRolePermissionBO.getRoleId();
        if(roleId==1){
            return Result.fail("超级管理员权限不允许修改");
        }else if(roleId==2){
            return Result.fail("商场管理员权限不允许修改");
        }else if(roleId==3){
            return Result.fail("推广管理员权限不允许修改");
        }
        roleService.postPermissions(postRolePermissionBO);

        return Result.ok();
    }

    @PostMapping("update")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-06 21:39
     * @version 1.0
     */
    public Result update(@RequestBody RoleUpDateBO roleUpDateBO) {
        int id = roleUpDateBO.getId();
        int count = roleService.update(roleUpDateBO);
        if (count == 200) {
            return Result.ok();
        }
        return Result.fail("用户名已被其他用户使用");
    }

    @RequestMapping("delete")
    /**
     * @description:
     * @author wangzhuo
     * @date 2022-01-06 21:39
     * @version 1.0
     */
    public Result delete(@RequestBody RoleDeleteBO roleDeleteBO) {
        roleService.delete(roleDeleteBO);
        return Result.ok();
    }
}
